#include <QyMath.h>
#include <QyGLViewWidget.h>
#include "QyDrawWallTool.h"
#include "QyToolRender.h"

/**************************************************************************************************/

class QyWallToolIndicator : public QyAbstractToolRenderItem {
public:
    QyWallToolIndicator() : width(1) {}

    void paint() const {

        glColor3d( 0, 0, 1 );
        glBegin(GL_QUADS);
        glVertex3dv(topLeft.p);
        glVertex3dv(topRight.p);
        glVertex3dv(bottomRight.p);
        glVertex3dv(bottomLeft.p);
        glEnd();

    }

    void update() {

        QyMath::Vector startVector = p1,
            endVector = p2;

        qreal direction = startVector.zDirection(endVector);
        qreal halfWidth = width / 2.0;

        topLeft = QyMath::Vector( QyMath::Vector::move( startVector, halfWidth, direction + 90.0 ) );
        topRight = QyMath::Vector( QyMath::Vector::move( startVector, halfWidth, direction - 90.0 ) );
        bottomRight = QyMath::Vector( QyMath::Vector::move( endVector, halfWidth, direction - 90.0 ) );
        bottomLeft = QyMath::Vector( QyMath::Vector::move( endVector, halfWidth, direction + 90.0 ) );

    }

    QyMath::Vector center() const {

        QyMath::Vector center = p2 - p1;

        center = p1 + center;

        return center;
    }

    QyMath::Vector p1,
                   p2,
                   topLeft,
                   topRight,
                   bottomRight,
                   bottomLeft;
    qreal width;

};

/**************************************************************************************************/

QyDrawWallTool::QyDrawWallTool( QyToolRender * render, QyGL::ViewWidget * target )
    : QObject(target), QyGL::InputController(target)
{

    p_indicator = new QyWallToolIndicator;
    p_indicator->setVisible(false);
    render->addItem(p_indicator);

}

QyDrawWallTool::~QyDrawWallTool() {

    render()->removeItem(p_indicator);
    delete p_indicator;

}

void QyDrawWallTool::mousePressd( const QyGL::MouseEvent & event ) {

    if ( !pressButtons() )
        return;

    p_indicator->setVisible( pressButtons() == Qt::LeftButton );
    p_indicator->p1 = event.glPos();
    p_indicator->p2 = p_indicator->p1;
    p_indicator->update();

}

void QyDrawWallTool::mouseMoved( const QyGL::MouseEvent & event ) {

    if ( isMousePressed() && pressButtons() == Qt::LeftButton ) {

        p_indicator->p2 = event.glPos();
        p_indicator->update();

    }

}

void QyDrawWallTool::mouseReleased( const QyGL::MouseEvent & event ) {

    if ( p_indicator->isVisible() ) {

        p_indicator->setVisible(false);
        QVector<QyMath::Vector> geometry(4);
        QyMath::Vector center = p_indicator->center();
        geometry[0] = p_indicator->topLeft - center;
        geometry[1] = p_indicator->topRight - center;
        geometry[2] = p_indicator->bottomRight - center;
        geometry[3] = p_indicator->bottomLeft - center;
        emit createWall( geometry, center );

    }

}
